package cn.edu.cug.cs.gtl.docsrv.dict.service.impl;

import cn.edu.cug.cs.gtl.docsrv.dict.dao.TableDao;
import cn.edu.cug.cs.gtl.docsrv.dict.dao.imp.TableDaoImpl;
import cn.edu.cug.cs.gtl.docsrv.dict.exception.BaseException;
import cn.edu.cug.cs.gtl.docsrv.dict.response.PaginatedValues;
import cn.edu.cug.cs.gtl.docsrv.dict.response.TableResponse;
import cn.edu.cug.cs.gtl.docsrv.dict.service.TableService;
import cn.edu.cug.cs.gtl.mybatis.mapper.dict.DictColInfo;
import cn.edu.cug.cs.gtl.mybatis.metadata.DictUtils;
import cn.edu.cug.cs.gtl.protos.TableInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Component
public class TableServiceImpl  implements TableService {
    @Autowired
    TableDao tableDao;

    @Override
    public TableResponse getTableData(String tableName, Integer pageNum, Integer pageSize) throws Exception {
        if (tableName==null || tableName.isEmpty()) {
            throw new BaseException("表名不能为空");
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 50;
        }
        List<Map> tableStructure = tableDao.getTableStructureV1(tableName);
        List<Map<String,Object>> tableData = tableDao.getTableData(tableName, pageNum, pageSize, tableStructure);

        List<Map> data = new ArrayList<>();
        for (Map<String,Object> rs: tableData) {
            Map map = new LinkedHashMap<>();
            for (Map columnStructure : tableStructure) {
                String a = (String) columnStructure.get("columnName");
                Object obj = rs.get(a);
                if(obj == null) map.put(a, "null");
                else map.put(a, obj.toString());
            }
            data.add(map);
        }
        Integer totalCount = tableDao.getTableDataCount(tableName);
        return new TableResponse(data, tableStructure, pageNum, pageSize, totalCount);
    }

    @Override
    public PaginatedValues<Map> getTableDataV1(String tableName, Integer pageNum, Integer pageSize) throws Exception {
        if (tableName==null || tableName.isEmpty()) {
            throw new BaseException("表名不能为空");
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 50;
        }
        List<Map> tableStructure = tableDao.getTableStructureV1(tableName);
        List<Map> tableData = tableDao.getTableDataV1(tableName, pageNum, pageSize, tableStructure);
        Integer totalCount = tableDao.getTableDataCount(tableName);
        return new PaginatedValues<>(tableData, tableStructure, pageNum, pageSize, totalCount);
    }

    @Override
    public List<Map> getTableStructureV1(String tableName) throws Exception {
        return tableDao.getTableStructureV1(tableName);
    }

    @Override
    public List<DictColInfo> getTableStructure(String tableName) throws Exception {
        return tableDao.getTableStructure(tableName);
    }

    @Override
    public TableInfo getTableInfo(String tableName) throws Exception {
        return tableDao.getTableInfo(tableName);
    }

    @Override
    public void addTableData(String tableName, List<Map> columnList) throws Exception {
        if (StringUtils.isEmpty(tableName)) {
            throw new BaseException("表名不能为空");
        }
        tableDao.addTableData(tableName, columnList);
    }

    @Override
    public void deleteTableData(String tableName, List<Map> columnList) throws Exception {
        if (StringUtils.isEmpty(tableName)) {
            throw new BaseException("表名不能为空");
        }
        tableDao.deleteTableData(tableName, columnList);
    }

    @Override
    public void updateTableData(String tableName, List<Map> columnList) throws Exception {
        if (StringUtils.isEmpty(tableName)) {
            throw new BaseException("表名不能为空");
        }
        tableDao.updateTableData(tableName, columnList);
    }

    @Override
    public void downloadTableBlobData(String tableName, String columnName,  List<Map> columnList, HttpServletResponse response) throws Exception {
        ServletOutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            outputStream = response.getOutputStream();
            inputStream = tableDao.getTableBlobData(tableName, columnName, columnList);
            byte[] buffer = new byte[2048];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
        } finally {
            if (inputStream != null){
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    public void setTableDao(TableDaoImpl t){
        this.tableDao=t;
    }
}
